
#include <BStyle>
#include <BApplication>
#include "IWindow.h"
#include "member_BDialog.h"
#include "member_BWidget.h"

using namespace BWE;

const char* member_BDialog::ButtonName(Button button)
{
	switch (button)
	{
	case Button_Close:
		return "close";
	case Button_Minimize:
		return "min";
	case Button_Maximize:
		return "max";
	case Button_Normalize:
		return "normal";
	default:
		return 0;
	}
	return 0;
}
Graph member_BDialog::ButtonGraph(Button button)
{
	switch (button)
	{
	case Button_Close:
		return Graph_Cross;
	case Button_Minimize:
		return Graph_Minimize;
	case Button_Maximize:
		return Graph_Maximize;
	case Button_Normalize:
		return Graph_Normalize;
	default:
		return Graph_None;
	}
	return Graph_None;
}

member_BDialog::member_BDialog(BDialog* dialog)
{
	boss = dialog;
	ignorePart = Part_None;
	resizePart = Part_None;
	result = 0;
	hoverTitle = false;
	pressed = false;
	moved = false;
	titleHeight = -1;
	titleSpace = 0;
	alignment.ia_align = Align_LeftCenter;
	buttonAlign = Align_RightCenter;
	titleIconSize.set(-1);
	buttonLayout = new BHBoxLayout();
	showState = Show_None;
	dirty = true;
}
member_BDialog::~member_BDialog()
{

}

bool member_BDialog::resizeale()
{
	ignorePart = Part_None;
	if (widget_member(boss)->parent)
	{
		if (BLayout* layout = widget_member(boss)->upperLayout)
			return false;
		return true;
	}
	if (widget_member(boss)->pixmap || widget_member(boss)->frameless)
		return true;
	return false;
}
void member_BDialog::freshRects()
{
	BSize buttonSize = buttonLayout->sizeHint();
	if (widget_member(boss)->pixmap || boss->parent() || boss->frameless())
	{
		int top = titleHeight;
		if (top < 0)
		{
			const BFont* font = boss->font();
			if (font && top < font->size())
				top = font->size();

			top = boss->perch(Part_Top);
			if (top < titleIconSize.height())
				top = titleIconSize.height();

			if (top < buttonSize.height())
				top = buttonSize.height();

		}
		buttonLayout->setVisible(true);
		boss->setPerch(Part_Top, top);
	}
	else
	{
		buttonLayout->setVisible(false);
		boss->setPerch(Part_Top, 0);
	}

	int w = boss->width();
	int h = boss->height();
	int b = boss->border();
	int b2 = b + b;

	titleRect = boss->rect(Part_Title);
	titleRect.top() += titleMargin.top();
	titleRect.left() += titleMargin.left();
	titleRect.right() -= titleMargin.right();
	titleRect.bottom() -= titleMargin.bottom();
	borderLeft.set(0, b, b, h - b - b);
	borderTop.set(b, 0, w - b - b, b);
	borderRight.set(w - b, b, b, h - b - b);
	borderBottom.set(b, h - b, w - b - b, b);
	cornerLeftTop.set(0, 0, b2, b2);
	cornerLeftBottom.set(0, h - b2, b2, b2);
	cornerRightTop.set(w - b, 0, b2, b2);
	cornerRightBottom.set(w - b2, h - b2, b2, b2);
	const BSizePolicy& sizePolicy = boss->sizePolicy();
	if (0 == (sizePolicy.widthPolicy() & Policy_Dynamic))
	{
		borderLeft.reset();
		borderRight.reset();
		cornerLeftTop.reset();
		cornerLeftBottom.reset();
		cornerRightTop.reset();
		cornerRightBottom.reset();
	}
	if (0 == (sizePolicy.heightPolicy() & Policy_Dynamic))
	{
		borderTop.reset();
		borderBottom.reset();
		cornerLeftTop.reset();
		cornerRightTop.reset();
		cornerLeftBottom.reset();
		cornerRightBottom.reset();
	}

	buttonLayout->setRect(titleRect.align(buttonAlign, buttonSize));
	buttonLayout->adjust();

	BRect iconArea(titleRect.x(), titleRect.y(), 0, titleRect.height());
	BSize realIconSize;
	BIcon* titleIcon = boss->titleIcon();
	if (titleIcon)
	{
		iconArea.setWidth(titleRect.height());
		realIconSize = titleIconSize;
		if (BImage* image = titleIcon->image(State_Normal))
		{
			if (realIconSize.width() < 0)
				realIconSize.width() = image->width();
			if (realIconSize.height() < 0)
				realIconSize.height() = image->height();
		}
		if (realIconSize.width() < 1)
			realIconSize.width() = titleRect.height();
		if (realIconSize.height() < 1)
			realIconSize.height() = titleRect.height();
		realIconSize = iconArea.wrap(realIconSize).size();
	}
	alignment.ia_imageSize = realIconSize;

	const BFont* font = boss->font();
	if (font && boss->title().size())
	{
		const BStyle* realStyle = boss->realStyle();
		if (realStyle)
			realStyle->translate(boss->title(), tr_title);
		else
			tr_title.clear();
		if (tr_title.size())
			alignment.ia_textSize = font->textSize(tr_title);
		else
			alignment.ia_textSize = font->textSize(boss->title());
	}
	alignment.adjust(titleRect);
}

void member_BDialog::slotTitleButtonClicked(BObject* object, const BValue& value)
{
	for (int i = 0; i < buttonLayout->count(); i++)
	{
		BWidget* widget = buttonLayout->widget(i);
		if (widget == object)
		{
			Button button = object->asset();
			if (button == Button_Close)
				boss->close(0);
			if (button == Button_Normalize)
				boss->show(Show_Normal);
			if (button == Button_Minimize)
				boss->show(Show_Minimized);
			if (button == Button_Maximize)
				boss->show(Show_Maximized);
			break;
		}
	}
}
void member_BDialog::slotParentResized(BObject* object, const BValue& value)
{
	if (showState == Show_Maximized)
	{
		const BSize& size = value;
		boss->setSize(size);
	}
}
